package ModelLayer;

import java.util.ArrayList;

/**
 * @author (Gruppe C) 
 * @version (2012.11.09)
 */
public class DvdContainer
{
    /*
     * Singleton - Only one instance allowed.
     */
    private static DvdContainer instance;
    private DvdContainer()
    {
        dvds = new ArrayList<Dvd>();
    }
    public static DvdContainer getInstance()
    {
        if (instance == null)
        {
            instance = new DvdContainer();
        }
        return instance;
    }
    
    
    // Fields
    private ArrayList<Dvd> dvds;
    
    /*
     * Return a dvd by id.
     * 
     * @param   id      ID of the dvd to be searched for.
     * @return          Returns the dvd with the specified id.
     */
    public Dvd findDvd(int id)
    {
        Dvd retDvd = null;
        
        // Loop through all dvds.
        for (Dvd dvd : dvds)
        {
            // If the current dvd's id is found - break the loop.
            if (dvd.getId() == id)
            {
                retDvd = dvd;
                break;
            }
        }
        
        return retDvd;
    }
    
    public Dvd findDvd(String title)
    {
        Dvd retDvd = null;
        
        for (Dvd dvd : dvds)
        {
            if (dvd.getTitle().equals(title))
            {
                return dvd;
            }
        }
        
        return retDvd;
    }
    
    /*
     * Adds a dvd to collection.
     * 
     * @param   dvd     DVD to be added.
     */
    public void addDvd(Dvd dvd)
    {
        dvds.add(dvd);
    }
    /*
     * Deletes a dvd from collection.
     * 
     * @param   dvd     DVD to be deleted.
     */
    public void deleteDvd(Dvd dvd)
    {
        dvds.remove(dvd);
    }
    
    /*
     * Return all dvd's in collection.
     * 
     * @return          Returns an array of dvds.
     */
    public ArrayList<Dvd> getAllDvds()
    {
        return dvds;
    }
}
